<template>
  <div class="dashboard-container">
    <!-- 侧边栏 -->
    <div class="sidebar" :class="{ 'collapsed': sidebarCollapsed }">
      <div class="sidebar-header">
        <h2 v-if="!sidebarCollapsed">图书管理系统</h2>
        <div class="logo" v-else>📚</div>
        <button class="toggle-btn neu-button" @click="toggleSidebar">
          <i :class="sidebarCollapsed ? 'el-icon-d-arrow-right' : 'el-icon-d-arrow-left'"></i>
        </button>
      </div>
      
      <div class="sidebar-menu">
        <router-link to="/dashboard/home" class="menu-item" active-class="active">
          <el-icon><HomeFilled /></el-icon>
          <span v-if="!sidebarCollapsed">首页</span>
        </router-link>
        
        <router-link to="/dashboard/books" class="menu-item" active-class="active">
          <el-icon><Reading /></el-icon>
          <span v-if="!sidebarCollapsed">图书管理</span>
        </router-link>
        
        <router-link to="/dashboard/borrow" class="menu-item" active-class="active">
          <el-icon><Document /></el-icon>
          <span v-if="!sidebarCollapsed">借阅管理</span>
        </router-link>
        
        <router-link v-if="isAdmin" to="/dashboard/users" class="menu-item" active-class="active">
          <el-icon><User /></el-icon>
          <span v-if="!sidebarCollapsed">用户管理</span>
        </router-link>
        
        <router-link to="/dashboard/statistics" class="menu-item" active-class="active">
          <el-icon><DataAnalysis /></el-icon>
          <span v-if="!sidebarCollapsed">数据统计</span>
        </router-link>
      </div>
    </div>
    
    <!-- 主内容区 -->
    <div class="main-content">
      <!-- 顶部导航栏 -->
      <div class="navbar neu-navbar">
        <div class="navbar-left">
          <h3>{{ currentRouteName }}</h3>
        </div>
        
        <div class="navbar-right">
          <el-dropdown trigger="click">
            <div class="user-info">
              <span>{{ user.name }}</span>
              <el-icon><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人信息</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      
      <!-- 路由视图 -->
      <div class="content-wrapper">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { HomeFilled, Reading, Document, User, DataAnalysis, ArrowDown } from '@element-plus/icons-vue'
import store from '../store'

const router = useRouter()
const route = useRoute()

// 用户信息
const user = computed(() => store.user || {})
const isAdmin = computed(() => store.isAdmin())

// 侧边栏状态
const sidebarCollapsed = ref(false)

// 切换侧边栏
const toggleSidebar = () => {
  sidebarCollapsed.value = !sidebarCollapsed.value
}

// 获取当前路由名称
const currentRouteName = computed(() => {
  const routeMap = {
    'Home': '首页',
    'Books': '图书管理',
    'Borrow': '借阅管理',
    'Users': '用户管理',
    'Statistics': '数据统计'
  }
  return routeMap[route.name] || ''
})

// 退出登录
const handleLogout = () => {
  store.clearUserInfo()
  router.push('/login')
}
</script>

<style scoped>
@import '../assets/styles/neumorphism.css';

.dashboard-container {
  display: flex;
  min-height: 100vh;
  background-color: var(--bg-color);
}

/* 侧边栏样式 */
.sidebar {
  width: 250px;
  background-color: var(--bg-color);
  box-shadow: 4px 0 10px var(--dark-shadow);
  transition: width 0.3s ease;
  z-index: 10;
  display: flex;
  flex-direction: column;
}

.sidebar.collapsed {
  width: 80px;
}

.sidebar-header {
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--dark-shadow);
}

.sidebar-header h2 {
  color: var(--primary-green);
  margin: 0;
  font-size: 1.2rem;
}

.logo {
  font-size: 24px;
}

.toggle-btn {
  padding: 5px;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sidebar-menu {
  display: flex;
  flex-direction: column;
  padding: 20px 0;
  flex: 1;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 12px 20px;
  color: var(--text-primary);
  text-decoration: none;
  transition: all 0.3s ease;
  margin-bottom: 5px;
}

.menu-item:hover {
  background-color: rgba(0, 0, 0, 0.05);
}

.menu-item.active {
  background: linear-gradient(90deg, var(--primary-green-light), transparent);
  color: var(--primary-green-dark);
  box-shadow: 
    inset 4px 0 0 var(--primary-green);
}

.menu-item i {
  margin-right: 10px;
  font-size: 18px;
}

.menu-item span {
  font-weight: 500;
}

/* 主内容区样式 */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

.navbar {
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.navbar-left h3 {
  margin: 0;
  color: var(--text-primary);
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 20px;
  box-shadow: 
    3px 3px 6px var(--dark-shadow),
    -3px -3px 6px var(--light-shadow);
}

.user-info span {
  margin-right: 5px;
  font-weight: 500;
}

.content-wrapper {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    height: 100vh;
    left: 0;
    top: 0;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }
  
  .sidebar.show {
    transform: translateX(0);
  }
  
  .main-content {
    margin-left: 0;
  }
}
</style>